package JFrame;


import JFrame.Admin.MailManager.AddMailJFrame;
import JFrame.Admin.UserManager.ModifyUserJFrame;
import statics.Data;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.sound.sampled.*;
/**
 * @className: start
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/27
 **/
public class StartJFrame extends JFrame implements ChangeListener, ActionListener {
    JButton start;
    JProgressBar bar;
    JLabel show;
    JPanel  pane1;
    Timer timer;
    Clip clip;
    AudioClip aau;

    public StartJFrame() throws IOException {
        super("121快递管理系统");
        init();
        this.setLocationRelativeTo(null);//窗体居中显示
        setVisible(true);

    }

    public void init() throws IOException {
        URL cb;
        File file = new File("src/statics/01.wav");
        cb = file.toURL();
        aau = Applet.newAudioClip(cb);//加载音频

        JLabel label=new JLabel(Data.start);
        label.setBounds(0,0,Data.start.getIconWidth(),Data.start.getIconHeight());
        this.setBounds(100, 100, 550, 550);

        this.getLayeredPane().add(label,-5);

        //获取frame的顶层容器,并设置为透明
        JPanel j=(JPanel)this.getContentPane();
        j.setOpaque(false);

        pane1 = new JPanel();

        start = new JButton("进入系统");
        start.addActionListener(this);


        bar = new JProgressBar();
        bar.setBounds(0,0,200,200);
        bar.setMinimum(0);
        bar.setMaximum(100);
        bar.setValue(0);
        bar.setStringPainted(true);
        bar.addChangeListener(this);
        bar.setPreferredSize(new Dimension(200, 35));
        int leave = bar.getValue();
        show = new JLabel("已完成：" + leave, JLabel.CENTER);

        pane1.add(start,BorderLayout.WEST);
        pane1.add(bar, BorderLayout.CENTER);
        pane1.add(show, BorderLayout.SOUTH);
        pane1.setVisible(true);
        this.add(pane1);
        this.setSize(Data.start.getIconWidth()+20,Data.start.getIconHeight()+50);
        this.setVisible(true);

        timer = new Timer(30, this);
    }


    @Override
    public void stateChanged(ChangeEvent e) {

        int value = bar.getValue();
        int leave = value;
        if(e.getSource() == bar) {
            show.setText("已完成："+ leave +"%");
        }
        if (leave == 100){
            new LoginJFrame();
            aau.play();
            this.dispose();
        }
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == start) {
            timer.start();
        } else if(e.getSource() == timer) {
            int value = bar.getValue();
            if(value <100 ) {
                value++;
                bar.setValue(value);
            }
        }
    }
    public static void main(String[] args) throws  IOException {
        new StartJFrame();
    }
}
